package cz.results.testperzist.guisl;



import java.util.Iterator;
import java.io.IOException;
import java.util.List;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;

import cz.results.testperzist.model.*;


public class GuiSLView extends JFrame {
    
    protected JTable table;
    private SlTableModel tm;
    private ModelDb4o model;
//    Startovka sl;
    
    public static void main(String[] args) {
        
//        Startovka sl = new Startovka("K1M");
//        sl.fillStartovka(10);
//        Model model = new Model("K1M");
        
        ModelDb4o model = new ModelDb4o("K1M");
        GuiSLView view = new GuiSLView(model);
        
        view.setVisible(true);
    }
    
    public GuiSLView(ModelDb4o model) {
        this.model = model;
        tm =  new SlTableModel(this);
        table = new JTable(tm);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 600);
        
        
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        
        JScrollPane jsp = new JScrollPane(table);
        pane.add(jsp, BorderLayout.CENTER);
        
        //panel tlacitek
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        pane.add(bottomPanel, BorderLayout.SOUTH);
        
        // EDIT - editace zavodnika
        JButton btnEdi = new JButton("edit");
        btnEdi.addActionListener(new editZavodnik());
        bottomPanel.add(btnEdi);
        
        
        // ADD - pridani noveho zavodnika
        JButton btnAdd = new JButton("add");
        btnAdd.addActionListener(new addZavodnik());
        bottomPanel.add(btnAdd);
        
        // DEL - odbrani aktualniho zavodnika
        JButton btnDelete = new JButton("del");
        btnDelete.addActionListener(new delZavodnik());
        bottomPanel.add(btnDelete);
        
        // ZAP - vymazani startovky
        JButton btnZap = new JButton("zap");
        btnZap.addActionListener(new zapStartovka());
        bottomPanel.add(btnZap);
        
        
        // REF - refresh tabulky
        JButton btnRefresh = new JButton("ref");
        btnRefresh.addActionListener(new refStartovka());
        bottomPanel.add(btnRefresh);
        
        
        // FIL - naplneni startovky testovacimi daty
        JButton btnImport = new JButton("fil");
        btnImport.addActionListener(new filStartovka());
        bottomPanel.add(btnImport);
    }
    
    public ModelDb4o getModel() {
        return model;
    }
    
    class refStartovka implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            model.refreshSL();
            table.updateUI();
        }
    }
    
    // edit zavodnika v SL
    class editZavodnik implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            Zavodnik tmpZavodnik = (Zavodnik) model.getStartovka().getSL().get(table.getSelectedRow());
            tmpZavodnik.setStc(tmpZavodnik.getStc()+1);
            tmpZavodnik.setJmeno("JMENO"+tmpZavodnik.getStc());
            tmpZavodnik.setPrijmeni("PRIJMENI"+tmpZavodnik.getStc());
            model.storeZavodnik(tmpZavodnik);
            table.updateUI();
        }
    }
    
    
    // pridani zavodnika do SL
    class addZavodnik implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            Zavodnik tmpZavodnik = Zavodnik.getTestZavodnik();
//            model.getStartovka().addZavodnik(tmpZavodnik);
            model.addZavodnik(tmpZavodnik);
//            model.getStartovka().dbWriteStartovka();
            table.updateUI();
        }
    }
    
    // smazani aktualniho zavodnika z SL
    class delZavodnik implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            Zavodnik tmpZavodnik = (Zavodnik) model.getStartovka().getSL().get(table.getSelectedRow());
//            model.getStartovka().delZavodnik(table.getSelectedRow());
            model.delZavodnik(tmpZavodnik);
            table.updateUI();
        }
    }
    
    // vymazani vsech zavodniku z SL
    class zapStartovka implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            model.getStartovka().zapStartovka();
            table.updateUI();
        }
    }
    
    class filStartovka implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            model.getStartovka().fillStartovka(10);
            table.updateUI();
        }
    }
    
}

class SlTableModel extends AbstractTableModel {
    
    private GuiSLView view;
    
    public SlTableModel(GuiSLView view) {
        this.view = view;
    }
    
    public int getRowCount() {
        return view.getModel().getStartovka().getSL().size();
    }
    
    public int getColumnCount() {
        return 4;
    }
    
    public Object getValueAt(int row, int col) {
        if (view.getModel().getStartovka().getSL().size() > 0) {
            if (col == 0) {
                return view.getModel().getStartovka().getZavodnik(row).getStc();
            } else if (col == 1) {
                return view.getModel().getStartovka().getZavodnik(row).getJmeno();
            } else if (col == 2) {
                return view.getModel().getStartovka().getZavodnik(row).getPrijmeni();
            } else if (col == 3) {
                return view.getModel().getStartovka().getZavodnik(row).getNar();
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
    
}
